<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="图集" prop="imageTypeId">
        <el-select
          v-model="queryParams.imageTypeId"
          placeholder="请选择图集"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="dict in imageTypeList"
            :key="dict.imageTypeId"
            :label="dict.imageTypeName"
            :value="dict.imageTypeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="labelId">
        <el-select
          v-model="queryParams.labelId"
          placeholder="请选择标签"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="dict in labelList"
            :key="dict.labelId"
            :label="dict.labelName"
            :value="dict.labelId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图片名称" prop="imageName">
        <el-input
          v-model="queryParams.imageName"
          placeholder="请输入图片名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.publish_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resource:image:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resource:image:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:image:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resource:image:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="imageList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图片ID" align="center" prop="imageId" />
      <el-table-column label="图集" align="center" prop="imageTypeName" />
      <el-table-column label="图片名称" align="center" prop="imageName" />
      <el-table-column label="图片" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.imageUrl"
            :isShowThumbnail="true"
            :width="50"
            :height="50"
          />
          <!--          <el-image :src="scope.row.imageUrl" />-->
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="labelName" />
      <el-table-column label="清单对象" align="center" prop="bridgeName" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="拍摄方向" align="center" prop="imageDirection" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否公开" align="center" prop="isPublic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isPublic" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.publish_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resource:image:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resource:image:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图片资源对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      :fullscreen="true"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图集" prop="imageTypeId">
              <el-select
                v-model="form.imageTypeId"
                placeholder="请选择图集"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in imageTypeList"
                  :key="dict.imageTypeId"
                  :label="dict.imageTypeName"
                  :value="dict.imageTypeId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="labelId">
              <el-select
                v-model="form.labelId"
                placeholder="请选择标签"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in labelList"
                  :key="dict.labelId"
                  :label="dict.labelName"
                  :value="dict.labelId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="imageName">
              <el-input v-model="form.imageName" placeholder="请输入图片名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="清单对象" prop="bridgeName">
              <el-select
                v-model="form.bridgeName"
                placeholder="请选择清单对象"
                @click.native="openPojectDialog()"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="拍摄方向" prop="imageDirection">
              <el-input v-model="form.imageDirection" placeholder="请输入拍摄方向" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公开" prop="isPublic">
              <el-select
                v-model="form.isPublic"
                placeholder="请选择是否公开"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="图片" prop="imageUrls" v-if="form.imageId == null">
            <image-upload v-model="form.imageUrls" :fileSize="20" />
          </el-form-item>
          <el-form-item v-else label="图片" prop="imageUrl">
            <image-upload v-model="form.imageUrl" :limit="1" :fileSize="20" />
          </el-form-item>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('0')">暂 存</el-button>
        <el-button type="primary" @click="submitForm('1')">发 布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查询桥梁列表 -->
    <el-dialog
      title="清单选择"
      :visible.sync="dialogVisible"
      :fullscreen="true"
      append-to-body
      width="700px"
      class="app-container search-table-box aidex-table"
      v-if="dialogVisible"
    >
      <!-- 搜索栏 -->
      <el-card
        shadow="never"
        style="margin-bottom: 12px"
        class="search_card"
        v-show="showSearch"
      >
        <div class="filter-container">
          <div class="search_box">
            <el-form
              :model="projectQueryParams"
              ref="queryForm1"
              v-show="showSearch"
              label-width="80px"
            >
              <el-row :gutter="16">
                <el-col :md="6">
                  <el-form-item label="桥梁名称" prop="projectName">
                    <el-input
                      v-model="projectQueryParams.bridgeName"
                      placeholder="请输入桥梁名称"
                      clearable
                      @keyup.enter.native="inputChange"
                    />
                  </el-form-item>
                </el-col>
                <el-col :md="6">
                  <el-form-item label="桥梁编号" prop="bridgeNum">
                    <el-input
                      v-model="projectQueryParams.bridgeNum"
                      placeholder="请输入桥梁编号"
                      clearable
                      @keyup.enter.native="inputChange"
                    />
                  </el-form-item>
                </el-col>
                <el-col :md="6"> </el-col>
                <el-col :md="6" align="right" style="float: right">
                  <el-form-item class="search_btn_box">
                    <el-button class="filter-item" type="primary" @click="inputChange"
                      >搜索</el-button
                    >
                    <el-button
                      class="filter-item"
                      style="margin-left: 8px"
                      @click="resetProjectQuery"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
      </el-card>
      <el-table
        stripe
        ref="tableRef"
        @row-click="rowClick"
        :data="bridgeList"
        v-loading="loading"
        :border="true"
        highlight-current-row
        style="width: 100%"
        :height="getInitTableHeight()"
      >
        <el-table-column label="桥梁编号" align="center" prop="bridgeNum" />
        <el-table-column label="桥梁名称" align="center" prop="bridgeName" width="180" />
        <el-table-column label="省" align="center" prop="province" />
        <el-table-column label="市" align="center" prop="city" />
        <el-table-column label="乡/行政区" align="center" prop="county" />
        <!--        <el-table-column label="桥梁年代" align="center" prop="bridgeAge">-->
        <!--          <template slot-scope="scope">-->
        <!--            <dict-tag :options="dict.type.sys_bridge_age" :value="scope.row.bridgeAge" />-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.publish_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <div slot="empty">
          <br />
          <svg-icon icon-class="search-none" style="font-size: 64px" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination
        v-show="projectTotal > 0"
        :total="projectTotal"
        :page.sync="projectQueryParams.pageNum"
        :limit.sync="projectQueryParams.pageSize"
        @pagination="inputChange"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listImage,
  getImage,
  delImage,
  addImage,
  updateImage,
} from "@/api/resource/image";
import {
  listLabel,
  getLabel,
  delLabel,
  addLabel,
  updateLabel,
} from "@/api/resource/label";
import {
  listImageType,
  getImageType,
  delImageType,
  addImageType,
  updateImageType,
} from "@/api/resource/imageType";
import {
  listList,
  getList,
  delList,
  addList,
  updateList,
  withdrawList,
  moveUpOrDown,
  pinToTopOrEnd,
} from "@/api/bussiness/list";

export default {
  name: "Image",
  dicts: ["publish_status", "sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      projectTotal: 0,
      // 图片资源表格数据
      imageList: [],
      // 图集列表表格数据
      imageTypeList: [],
      bridgeList: [],
      // 标签表格数据
      labelList: [],
      imageSuggest: "高250像素，宽420像素",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      projectQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imageTypeId: null,
        imageName: null,
        author: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        imageTypeId: [{ required: true, message: "图集ID不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "change" }],
      },
    };
  },
  created() {
    this.getTypeList();
    this.getLabelList();
    this.getList();
  },
  methods: {
    async getTypeList() {
      var para = {
        pageNum: 1,
        pageSize: 1000,
      };
      await listImageType(para).then((response) => {
        this.imageTypeList = response.rows;
        this.total = response.total;
      });
    },
    async getLabelList() {
      var para = {
        pageNum: 1,
        pageSize: 1000,
      };
      await listLabel(para).then((response) => {
        this.labelList = response.rows;
        this.total = response.total;
      });
    },
    /** 查询图片资源列表 */
    getList() {
      this.loading = true;
      listImage(this.queryParams).then((response) => {
        this.imageList = response.rows;
        // for(var i=0; i<this.imageList.length; i++ ){
        //   this.imageList[i].imageUrl =this.imageList[i].imageUrl +'?x-oss-process=image/resize,m_fill,h_50,w_50';
        // }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        imageId: null,
        imageTypeId: null,
        imageName: null,
        labelId: null,
        author: null,
        status: null,
        isTop: null,
        isBottom: null,
        orderNum: null,
        imageUrl: null,
        imageUrls: null,
        createBy: null,
        isPublic: "Y",
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.imageId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图片资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const imageId = row.imageId || this.ids;
      getImage(imageId).then((response) => {
        this.form = response.data;
        this.form.labelId = parseInt(this.form.labelId);
        this.open = true;
        this.title = "修改图片资源";
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.status = status;
          if (this.form.imageId != null) {
            updateImage(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addImage(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const imageIds = row.imageId || this.ids;
      this.$modal
        .confirm('是否确认删除图片资源编号为"' + imageIds + '"的数据项？')
        .then(function () {
          return delImage(imageIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "resource/image/export",
        {
          ...this.queryParams,
        },
        `image_${new Date().getTime()}.xlsx`
      );
    },
    openPojectDialog() {
      this.dialogVisible = true;
      this.bridgeList = [];
      this.inputChange();
    },
    async inputChange() {
      this.projectQueryParams.status = "1";
      listList(this.projectQueryParams).then((response) => {
        this.bridgeList = response.rows;
        this.projectTotal = response.total;
      });
    },
    resetProjectQuery() {
      this.projectQueryParams = this.$options.data().projectQueryParams;
      this.inputChange();
    },
    rowClick(row) {
      if (row) {
        this.form.checkListId = row.checkListId;
        this.form.bridgeName = row.bridgeName;
        this.dialogVisible = false;
      }
    },
  },
};
</script>
